package com.example.activitytest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 在活动中加载布局  一个活动一个布局
        setContentView(R.layout.first_layout);
//        返回View对象，需要向下转型
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
//              隐式Intent
//                Intent intent = new Intent("com.example.activitytest.ACTION_START");
//                intent.addCategory("com.example.activitytest.MY_CATEGORY");
//               显式Intent
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                String data = "User123";
                // 向下传递数据
//                intent.putExtra("extra_data", data);
//                startActivity(intent);
//                启动活动且返回数据：向上传递数据
                intent.putExtra("extra_data", data);
                // 启动的这个活动 销毁之后会回调上一个活动（即当前活动）的onActivityResult() 方法
                startActivityForResult(intent, 1);
            }
        });
        // 跳转活动3
        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, ThirdActivity.class);
                startActivity(intent);
            }
        });
        // 跳转
        Button button3 = (Button) findViewById(R.id.button_3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, ParentFrameActivity.class);
                startActivity(intent);
            }
        });
        // 获取按钮
        // 隐式Intent来启动系统浏览器打开网页
        Button bw = (Button) findViewById(R.id.bw);
        bw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
        // 获取拨号按钮
        Button btel = (Button) findViewById(R.id.tel);
        btel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intt = new Intent(Intent.ACTION_DIAL);
                intt.setData(Uri.parse("tel:10088"));
                startActivity(intt);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String  returnedData = data.getStringExtra("data_return");
                    Log.d("FirstActivity", returnedData);
                }
                break;
            default:
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.add_item:
                Toast.makeText(this,"点击了添加菜单",Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this, "点击了移除菜单", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}